import { defineStore } from 'pinia'
import http from '../utils/http'

export const useCarStore = defineStore('car', {
  state: () => ({
    cars: [],
    brands: [],
    currentCar: null,
    loading: false,
    error: null
  }),
  
  getters: {
    getCars: (state) => state.cars,
    getBrands: (state) => state.brands,
    getCurrentCar: (state) => state.currentCar,
    isLoading: (state) => state.loading
  },
  
  actions: {
    async fetchCars(params = {}) {
      this.loading = true
      try {
        const response = await http.get('/car/cars', { params })
        this.cars = response
        this.error = null
        return { success: true, data: response }
      } catch (error) {
        this.error = error.response?.data?.message || '获取汽车列表失败'
        return { success: false, message: this.error }
      } finally {
        this.loading = false
      }
    },
    
    async fetchBrands() {
      this.loading = true
      try {
        const response = await http.get('/car/brands')
        this.brands = response
        this.error = null
        return { success: true, data: response }
      } catch (error) {
        this.error = error.response?.data?.message || '获取品牌列表失败'
        return { success: false, message: this.error }
      } finally {
        this.loading = false
      }
    },
    
    async fetchCarById(carId) {
      this.loading = true
      try {
        const response = await http.get(`/car/cars/${carId}`)
        this.currentCar = response
        this.error = null
        return { success: true, data: response }
      } catch (error) {
        this.error = error.response?.data?.message || '获取汽车详情失败'
        return { success: false, message: this.error }
      } finally {
        this.loading = false
      }
    },
    
    async createCar(carData) {
      this.loading = true
      try {
        const response = await http.post('/car/cars', carData)
        // 更新列表
        this.cars.push(response.car)
        this.error = null
        return { success: true, data: response }
      } catch (error) {
        this.error = error.response?.data?.message || '创建汽车失败'
        return { success: false, message: this.error }
      } finally {
        this.loading = false
      }
    },
    
    async updateCar(carId, carData) {
      this.loading = true
      try {
        const response = await http.put(`/car/cars/${carId}`, carData)
        // 更新列表和当前车辆
        const index = this.cars.findIndex(car => car.id === carId)
        if (index !== -1) {
          this.cars[index] = response.car
        }
        if (this.currentCar && this.currentCar.id === carId) {
          this.currentCar = response.car
        }
        this.error = null
        return { success: true, data: response }
      } catch (error) {
        this.error = error.response?.data?.message || '更新汽车失败'
        return { success: false, message: this.error }
      } finally {
        this.loading = false
      }
    },
    
    async deleteCar(carId) {
      this.loading = true
      try {
        await http.delete(`/car/cars/${carId}`)
        // 从列表中移除
        this.cars = this.cars.filter(car => car.id !== carId)
        if (this.currentCar && this.currentCar.id === carId) {
          this.currentCar = null
        }
        this.error = null
        return { success: true }
      } catch (error) {
        this.error = error.response?.data?.message || '删除汽车失败'
        return { success: false, message: this.error }
      } finally {
        this.loading = false
      }
    },
    
    async createBrand(brandData) {
      this.loading = true
      try {
        const response = await http.post('/car/brands', brandData)
        // 更新品牌列表
        this.brands.push(response.brand)
        this.error = null
        return { success: true, data: response }
      } catch (error) {
        this.error = error.response?.data?.message || '创建品牌失败'
        return { success: false, message: this.error }
      } finally {
        this.loading = false
      }
    }
  }
})